package com.cloud.bc.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.cloud.bc.model.entity.BcEmailTemplates;
import com.cloud.bc.model.entity.BcSmsTemplates;
import com.cloud.bc.mapper.BcSmsTemplatesMapper;
import com.cloud.bc.service.BcSmsTemplatesService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.model.enums.RedisKeyEnum;
import com.cloud.redis.config.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * 短信模版
 *
 * @author douhaichao code generator
 * @date 2021-01-29 21:46:33
 */
@Service
@AllArgsConstructor
public class BcSmsTemplatesServiceImpl extends ServiceImpl<BcSmsTemplatesMapper, BcSmsTemplates> implements BcSmsTemplatesService {
    private final RedisUtils redisUtils;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcSmsTemplates selectOne(BcSmsTemplates req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcSmsTemplates>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcSmsTemplates> selectAll(BcSmsTemplates req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcSmsTemplates> selectPage(Page page, BcSmsTemplates req) {
        IPage<BcSmsTemplates> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcSmsTemplates>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcSmsTemplates> selectPageByLike(Page page, BcSmsTemplates req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (!StrUtil.isEmpty(req.getAlias())) {
            queryWrapper.like("alias", req.getAlias());
        }

        if (!StrUtil.isEmpty(req.getName())) {
            queryWrapper.like("name", req.getName());
        }

        if (!StrUtil.isEmpty(req.getValue())) {
            queryWrapper.like("value", req.getValue());
        }

        if (!StrUtil.isEmpty(req.getTempId())) {
            queryWrapper.like("temp_id", req.getTempId());
        }

        if (!StrUtil.isEmpty(req.getWay())) {
            queryWrapper.like("way", req.getWay());
        }

        if (!StrUtil.isEmpty(req.getParams())) {
            queryWrapper.like("params", req.getParams());
        }

        if (!StrUtil.isEmpty(req.getModule())) {
            queryWrapper.like("module", req.getModule());
        }

        if (null != req.getType()) {
            queryWrapper.eq("type", req.getType());
        }

        if (null != req.getIsSwitch()) {
            queryWrapper.eq("is_switch", req.getIsSwitch());
        }
        IPage<BcSmsTemplates> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcSmsTemplates req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<BcSmsTemplates> selectAllCache() {
        String key = RedisKeyEnum.bcSmsTemplateRedisKey.getTitle();
        Object o = redisUtils.get(key);
        String data = Convert.toStr(o);
//        if (StrUtil.isNotEmpty(data)) {
//            return JSONUtil.toList(data, BcSmsTemplates.class);
//        }
        QueryWrapper queryWrapper = new QueryWrapper();
        //只查已开启的模板
        queryWrapper.eq("is_switch", 1);
        List<BcSmsTemplates> bcSmsTemplates = baseMapper.selectList(queryWrapper);
        String s = JSONUtil.toJsonStr(bcSmsTemplates);
        redisUtils.set(key, s);
        return bcSmsTemplates;
    }
}
